import { Button, Card, Form, Input, InputNumber, Modal, Popconfirm, Space } from 'antd';
import Table, { ColumnsType } from 'antd/es/table';
import { TableRowSelection } from 'antd/es/table/interface';
import { useEffect } from 'react';

import { IconButton, Iconify } from '@/components/icon';
import { useCommon } from '@/hooks/common';

export default function LevelPage() {
  const uri = 'member-level';
  const cateName = '会员等级';
  const formParams = {
    deposit: 0,
    discount: 0,
    firstRecharge: 0,
    gmv: 0,
    levelName: '',
    tradeNumber: 0,
  };
  const {
    data = {},
    ModalPros,
    onCreate,
    doDel,
    onEdit,
    handleChange,
  } = useCommon({}, uri, {
    id: 0,
    ...formParams,
  });

  const columns: ColumnsType<any> = [
    { title: '等级名称', dataIndex: 'levelName', width: 80 },
    { title: '首次充值', dataIndex: 'firstRecharge', align: 'center', width: 60 },
    { title: '保证金', dataIndex: 'deposit', align: 'center', width: 60 },
    { title: '近30天交易次数', dataIndex: 'tradeNumber', align: 'center', width: 60 },
    { title: '近30天交易金额', dataIndex: 'gmv', align: 'center', width: 60 },
    { title: '折扣率', dataIndex: 'discount', align: 'center', width: 60 },
    {
      title: '操作',
      key: 'operation',
      align: 'center',
      width: 100,
      render: (_, record) => (
        <div className="flex w-full justify-center text-gray">
          <IconButton onClick={() => onEdit(record)}>
            <Iconify icon="solar:pen-bold-duotone" size={18} />
          </IconButton>
          <Popconfirm
            title="确认删除该数据？"
            okText="确定"
            cancelText="取消"
            placement="left"
            onConfirm={() => doDel(record.id)}
          >
            <IconButton>
              <Iconify icon="mingcute:delete-2-fill" size={18} className="text-error" />
            </IconButton>
          </Popconfirm>
        </div>
      ),
    },
  ];

  // rowSelection objects indicates the need for row selection
  const rowSelection: TableRowSelection<any> = {
    onChange: (selectedRowKeys, selectedRows) => {
      console.log(`selectedRowKeys: ${selectedRowKeys}`, 'selectedRows: ', selectedRows);
    },
    onSelect: (record, selected, selectedRows) => {
      console.log(record, selected, selectedRows);
    },
    onSelectAll: (selected, selectedRows, changeRows) => {
      console.log(selected, selectedRows, changeRows);
    },
  };

  return (
    <Space direction="vertical" size="large" className="w-full">
      {/* <Card>
        <Form form={searchForm} onFinish={onSearch}>
          <Row gutter={[16, 16]}>
            <Col span={8} lg={6}>
              <Form.Item<any> label="分类" name="groupId" className="!mb-0">
                <Cascader
                  showSearch
                  options={cateList}
                  fieldNames={{ label: 'name', value: 'id', children: 'sonList' }}
                />
              </Form.Item>
            </Col>
            <Col span={8} lg={6}>
              <Form.Item<any> label="名称" name="name" className="!mb-0">
                <Input />
              </Form.Item>
            </Col>
            <Col span={8} lg={6}>
              <Form.Item<any> label="状态" name="disable" className="!mb-0">
                <Select
                  options={[
                    { label: '全部', value: '' },
                    { label: '已上架', value: 0 },
                    { label: '已下架', value: 1 },
                  ]}
                />
              </Form.Item>
            </Col>
            <Col span={24} lg={12}>
              <div className="flex justify-end">
                <Button onClick={onSearchFormReset}>重置</Button>
                <Button type="primary" htmlType="submit" className="ml-4">
                  查询
                </Button>
              </div>
            </Col>
          </Row>
        </Form>
      </Card> */}

      <Card
        title={cateName}
        extra={
          <div className="flex flex-row gap-[10px]">
            <Button type="primary" onClick={() => onCreate({ ...formParams })}>
              新增
            </Button>
          </div>
        }
      >
        <Table
          rowKey="id"
          size="small"
          pagination={{ total: data.totalCount }}
          scroll={{ x: 'max-content' }}
          columns={columns}
          dataSource={data.list}
          rowSelection={{ ...rowSelection }}
          onChange={handleChange}
        />
      </Card>

      <CommonModal {...ModalPros} />
    </Space>
  );
}

function CommonModal({ title, show, formValue, onOk, onCancel }: any) {
  const [form] = Form.useForm();
  useEffect(() => {
    form.setFieldsValue({ ...formValue });
  }, [formValue, form]);
  return (
    <Modal
      title={title}
      open={show}
      onOk={() => onOk({ ...formValue, ...form.getFieldsValue() })}
      onCancel={onCancel}
    >
      <Form
        initialValues={formValue}
        form={form}
        labelCol={{ span: 4 }}
        wrapperCol={{ span: 18 }}
        layout="horizontal"
      >
        <Form.Item<any> label="等级名称" name="levelName" required>
          <Input />
        </Form.Item>
        <Form.Item<any> label="保证金" name="deposit" required>
          <InputNumber />
        </Form.Item>
        <Form.Item<any> label="首次充值金额" name="firstRecharge" required>
          <InputNumber />
        </Form.Item>
        <Form.Item<any> label="交易金额" name="gmv" required>
          <InputNumber />
        </Form.Item>
        <Form.Item<any> label="交易次数" name="tradeNumber" required>
          <InputNumber />
        </Form.Item>
        <Form.Item<any> label="会员折扣" name="discount" required>
          <InputNumber style={{ width: '100%' }} />
        </Form.Item>
      </Form>
    </Modal>
  );
}
